package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.ScoreRecord;
import com.sims.dao.IScoreRecordDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * ScoreRecord entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sims.bean.ScoreRecord
 * @author MyEclipse Persistence Tools
 */

public class ScoreRecordDAO extends HibernateDaoSupport implements IScoreRecordDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ScoreRecordDAO.class);
	// property constants
	public static final String SID = "sid";
	public static final String COURSE = "course";
	public static final String SCORE = "score";
	public static final String TYPE = "type";
	public static final String STIME = "stime";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#save(com.sims.bean.ScoreRecord)
	 */
	@Override
	public void save(ScoreRecord transientInstance) {
		log.debug("saving ScoreRecord instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#delete(com.sims.bean.ScoreRecord)
	 */
	@Override
	public void delete(ScoreRecord persistentInstance) {
		log.debug("deleting ScoreRecord instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findById(java.lang.Integer)
	 */
	@Override
	public ScoreRecord findById(java.lang.Integer id) {
		log.debug("getting ScoreRecord instance with id: " + id);
		try {
			ScoreRecord instance = (ScoreRecord) getHibernateTemplate().get(
					"com.sims.bean.ScoreRecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findByExample(com.sims.bean.ScoreRecord)
	 */
	@Override
	public List findByExample(ScoreRecord instance) {
		log.debug("finding ScoreRecord instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ScoreRecord instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ScoreRecord as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findBySid(java.lang.Object)
	 */
	@Override
	public List findBySid(Object sid) {
		return findByProperty(SID, sid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findByCourse(java.lang.Object)
	 */
	@Override
	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findByScore(java.lang.Object)
	 */
	@Override
	public List findByScore(Object score) {
		return findByProperty(SCORE, score);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findByType(java.lang.Object)
	 */
	@Override
	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}
	
	public List findByTime(Object stime) {
		return findByProperty(STIME, stime);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all ScoreRecord instances");
		try {
			String queryString = "from ScoreRecord";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#merge(com.sims.bean.ScoreRecord)
	 */
	@Override
	public ScoreRecord merge(ScoreRecord detachedInstance) {
		log.debug("merging ScoreRecord instance");
		try {
			ScoreRecord result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#attachDirty(com.sims.bean.ScoreRecord)
	 */
	@Override
	public void attachDirty(ScoreRecord instance) {
		log.debug("attaching dirty ScoreRecord instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IScoreRecordDAO#attachClean(com.sims.bean.ScoreRecord)
	 */
	@Override
	public void attachClean(ScoreRecord instance) {
		log.debug("attaching clean ScoreRecord instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IScoreRecordDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IScoreRecordDAO) ctx.getBean("ScoreRecordDAO");
	}
}